package repository

import (
	"encoding/json"
	"gorm.io/gorm"
	"guliedu/model"
	"guliedu/utils"
	"mime/multipart"
	"time"
)

type IEduTeacherRepository interface {
	// 查询单个教师信息
	SelectByID(id string) (*model.EduTeacher, error)
	// 查询所有教师信息
	SelectAll() ([]*model.EduTeacher, error)
	// 删除教师信息
	Delete(id string) error
	// 查询所有记录数
	SelectAllCount() (int64, error)
	// 查询一页教师信息
	SelectPage(pageSize int, page int, name string, level int) ([]*model.EduTeacher, error)
	// 添加教师信息
	Insert(t *model.EduTeacher) error
	// 更新教师信息
	Update(t *model.EduTeacher) error
	// 添加教师头像
	UploadProfile(file multipart.File, fileSize int64) (string, int)
	// 查询首页热门教师信息
	SelectIndexHotTeacher() ([]model.EduTeacher, error)
	// 分页查询前台名师
	SelectFrontTeacherInfoList(page int, pageSze int) ([]model.EduTeacher, error)
	// 缓存首页教师信息
	CacheTeacher([]model.EduTeacher) error
	// 获取缓存
	GetCacheTeacher() []model.EduTeacher
}

func NewIEduTeacherRepository() IEduTeacherRepository {
	return &EduTeacherRepository{mysqlDb: db}
}

type EduTeacherRepository struct {
	mysqlDb *gorm.DB
}

func (e *EduTeacherRepository) SelectByID(id string) (*model.EduTeacher, error) {
	t := &model.EduTeacher{}
	err := db.Where("id = ?", id).Find(&t).Error
	return t, err
}

func (e *EduTeacherRepository) SelectAll() ([]*model.EduTeacher, error) {
	var eduTeachers []*model.EduTeacher
	err := db.Find(&eduTeachers).Error
	return eduTeachers, err
}

func (e *EduTeacherRepository) Delete(id string) error {
	err := db.Where("id = ?", id).Delete(&model.EduTeacher{}).Error
	return err
}

func (e *EduTeacherRepository) SelectAllCount() (int64, error) {
	var count int64
	err := db.Find(&model.EduTeacher{}).Count(&count).Error
	return count, err
}

func (e *EduTeacherRepository) SelectPage(pageSize int, page int, name string, level int) ([]*model.EduTeacher, error) {
	var eduTeachers []*model.EduTeacher
	err := db.Limit(pageSize).Offset((page-1)*pageSize).Scopes(eduTeacherQueryWithName(name), eduTeacherQueryWithLevel(level)).Order("created_at").Find(&eduTeachers).Error
	return eduTeachers, err
}

func eduTeacherQueryWithName(name string) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if name != "" {
			return db.Where("name = ?", name)
		}
		return db
	}
}

func eduTeacherQueryWithLevel(level int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if level != 0 {
			return db.Where("level = ?", level)
		}
		return db
	}
}

func (e *EduTeacherRepository) Insert(t *model.EduTeacher) error {
	return db.Create(&t).Error
}

func (e *EduTeacherRepository) Update(t *model.EduTeacher) error {
	return db.Save(&t).Error
}

func (e *EduTeacherRepository) UploadProfile(file multipart.File, fileSize int64) (string, int) {
	return utils.UploadFile(file, fileSize)
}

func (e *EduTeacherRepository) SelectIndexHotTeacher() (teahers []model.EduTeacher, err error) {
	err = e.mysqlDb.Order("sort desc").Limit(4).Find(&teahers).Error
	return
}

func (e *EduTeacherRepository) SelectFrontTeacherInfoList(page int, pageSize int) (teachers []model.EduTeacher, err error) {
	err = e.mysqlDb.Offset((page - 1) * pageSize).Limit(pageSize).Order("sort desc").Find(&teachers).Error
	return
}

func (e *EduTeacherRepository) CacheTeacher(teachers []model.EduTeacher) error {
	teachersJson, _ := json.Marshal(teachers)
	return client.Set("teacher:index", teachersJson, time.Hour*24).Err()
}

func (e *EduTeacherRepository) GetCacheTeacher() (teachers []model.EduTeacher) {
	teachersJson, _ := client.Get("teacher:index").Result()
	json.Unmarshal([]byte(teachersJson), &teachers)
	return
}
